package Dao;

import DataClass.RegisteredUsers;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RegisteredUsersDao {
    private static PreparedStatement preparedStatement = null;

    public static List<RegisteredUsers> getRegisterUsers() throws SQLException {
        List<RegisteredUsers> list = new ArrayList<RegisteredUsers>();
        ResultSet data;
        try{
            data = RegisteredUsersDao.getRegisteredUsers();
        }catch (SQLException e){
            System.out.println("注册用户失败 from Dao");
            throw new SQLException();
        }

        while(data.next()){
            Integer RU_id = data.getInt("RU_id");
            String RU_password = data.getString("RU_password");
            String RU_type = data.getString("RU_type");
            String RU_no = data.getString("RU_no");

            list.add(new RegisteredUsers(RU_id, RU_password, RU_type, RU_no));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }

    /**
     * 获取注册用户
     */
    public static ResultSet getRegisteredUsers() throws SQLException {

        if(preparedStatement == null)
        {
            String sql = "select * from Registered_Users";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            preparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet result = preparedStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}
